#!/usr/bin/env python

import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import datetime
from django.utils import simplejson

from model import StudentVoice
from model import ClassState
from model import Lecture

class MyHandler(webapp.RequestHandler):
    
    def get(self):
#        voiceCount = [];
        lecture = Lecture.get(self.request.get('lectureKey'))
        classState = db.Query(ClassState).filter('lecture = ', lecture).get()

        if not classState:
            self.response.out.write("No class state")
            return

        voiceTotal = 0
        voiceDontUnderstand = 0
        isQuizOn = classState.isQuizOn
        quizQuestionKey = None
        topicIndex = None
        topicKey = None
        topicTime = classState.currentTopicTime
        test = None
        #class has started
        if classState.currentTopic:
            voices = db.Query(StudentVoice).filter('topic = ', classState.currentTopic)
            test = voices.count()
            voiceTotal = voices.count()
            voiceDontUnderstand = voices.filter('understand =', False).count()
            topicIndex = classState.currentTopic.index
            topicKey = str(classState.currentTopic.key())
        
        #quiz has started
        if classState.isQuizOn:
            quizQuestionKey = str(classState.quizQuestion.key())
        #.filter('Understand = ', False).count()
        # voiceCount = [voices.Count(), voices.filter('Understand = ', False).count()]
            
        # change! - modify by class id.
        # numOfStuds=db.Query(StudentVoice).count()
            
        # change! - get real subjects
#         classState = db.Query(ClassState)
# #        db.delete(classState)
#         classState = classState.get()

        dirResponse = dict(test = test, voiceTotal = voiceTotal, voiceDontUnderstand = voiceDontUnderstand, isQuizOn = isQuizOn, quizQuestionKey = quizQuestionKey, topicIndex = topicIndex, topicKey = topicKey, topicTime = topicTime)

        # dirResponse = dict(studentCols = voiceCount, numOfStudents=numOfStuds, subjectInd=subjectKey, isQuizOn=isQuizOn, quizKey = quizQuestionKey)
        
        self.response.out.write(simplejson.dumps(dirResponse))                        

def main():
    app = webapp.WSGIApplication([
        (r'.*', MyHandler)], debug=True)
    wsgiref.handlers.CGIHandler().run(app)

if __name__ == "__main__":
    main()

##!/usr/bin/env python
#
#import wsgiref.handlers
#from google.appengine.ext import db
#from google.appengine.ext import webapp
#from google.appengine.ext.webapp import template
#from django.utils import simplejson
#import datetime
#from model import Voice
#
#class MyHandler(webapp.RequestHandler):
#    def get(self):
#        self.response.out.write("test");
#        self.
##        self.response.out.write("json page works");
#        voiceCount = ();
#        for i in range(10):
#            x_minutes_ago = datetime.datetime.now() - datetime.timedelta(seconds=i * 10);
#            plus_one_minutes_ago = datetime.datetime.now() - datetime.timedelta(seconds=(i+1) * 10);
#            voiceCount += (simplejson.loads(
#                        db.Query(Voice)
#                        .filter('when > ', plus_one_minutes_ago)
#                        .filter('when < ', x_minutes_ago)
#                        .count()) , );
##                        
##        
##        #self.response.out.write(simplejson.dumps(voiceCount));